<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts"/>
        <hr>
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :treeNode="node"
      @addDepts="getDepartments"
    ></AddDept>
  </div>
</template>

<script>
import AddDept from "@/views/departments/components/add-dept";
import treeTools from "@/views/departments/components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils/index.js";
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      showDialog: false,
      node: {},
      company: {},
      departs: [],
      defaultProps: {
        label: "name" // 表示 从这个属性显示内容
      },
      loading: false // 控制进度条的显示隐藏
    };
  },
  created() {
    this.getDepartments();
  },

  methods: {
    async getDepartments() {
      const res = await getDepartments();
      this.company = { name: res.companyName, manager: "负责人" };
      this.departs = tranListToTreeData(res.depts, "");
      this.company = { name: res.companyName, manager: "负责人", id: "" };
      this.loading = false;
    },
    addDepts(node) {
      this.showDialog = true; // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node;
      // console.log(this.node);
    },
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true;
      this.node = node; // 赋值操作的节点
      this.$refs.addDept.getDepartDetail(node.id);
    }
    // 获取部门详情
    // async getDepartDetail(id) {
    //   this.formData = await getDepartDetail(id);
    // }
  }
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>;